package com.xyhuo.leetcode.december;

/**
 * 字典序最小回文串
 * https://leetcode.cn/problems/lexicographically-smallest-palindrome
 *
 * @time 2023/12/13
 */
public class MakeSmallestPalindrome {

//    public static void main(String[] args) {
//        MakeSmallestPalindrome msp = new MakeSmallestPalindrome();
//        msp.makeSmallestPalindrome("abcd");
//    }

    public String makeSmallestPalindrome(String s) {
        int length = s.length();
        int halfLen = length / 2;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < halfLen; i++) {
            if (charArray[i] != charArray[length - 1 - i]) {
                if (charArray[i] < charArray[length - 1 - i]) {
                    charArray[length - 1 - i] = charArray[i];
                } else {
                    charArray[i] = charArray[length - 1 - i];
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (char c : charArray) {
            sb.append(c);
        }
        return sb.toString();
    }
}
